<script setup>
import { artGetChannelsService, delChnnelService } from '@/api/article'
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import channelEdit from '@/views/article/components/ChannelEdit.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
const channelList = ref([])
const loading = ref(true)
const dialog = ref()
// 获取文章
const getChannelList = async () => {
  loading.value = true
  const res = await artGetChannelsService()
  channelList.value = res.data.data
  loading.value = false
  //   console.log(channelList.value)
}
getChannelList()
// 编辑文章
const onEditChannel = (row) => {
  console.log(row)
  dialog.value.open({ row })
}
// 删除文章
const onDelChannel = async (row) => {
  // console.log(row, $index)
  await ElMessageBox.confirm('你确认要删除该分类吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await delChnnelService(row.id)
  ElMessage.success('删除成功')
  // 重新渲染
  getChannelList()
}
// 添加文章
const onAddChannel = () => {
  dialog.value.open({})
}

// 添加和修改数据后重新渲染表格
const onSuccess = () => {
  getChannelList()
}
</script>
<template>
  <page-container title="文章分类">
    <template #extra
      ><el-button type="primary" @click="onAddChannel"
        >添加分类</el-button
      ></template
    >
    <!-- 具名插槽 -->
    <el-table
      v-loading="loading"
      :data="channelList"
      stripe
      style="width: 100%"
    >
      <el-table-column
        type="index"
        prop="date"
        label="序号"
        width="150px"
      ></el-table-column>
      <el-table-column prop="cate_name" label="分类名称"></el-table-column>
      <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
      <el-table-column prop="date" label="操作" width="150px">
        <!-- row 就是channelList 的一项，  $index 下标 -->
        <template #default="{ row, $index }">
          <el-button
            :icon="Edit"
            type="primary"
            @click="onEditChannel(row, $index)"
            circle
            plain
          ></el-button>
          <el-button
            :icon="Delete"
            type="danger"
            @click="onDelChannel(row, $index)"
            circle
            plain
          ></el-button>
        </template>
      </el-table-column>

      <!-- 如果没有数据 -->
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>

    <!-- 弹窗组件 -->
    <channel-edit ref="dialog" @success="onSuccess"></channel-edit>
  </page-container>
</template>

<style lang="scss" scoped></style>
